package co.za.itweb.itwebpro;

import java.util.Arrays;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

@SuppressLint({ "ParserError" })
public class SearchAdapter extends BaseAdapter {

	private SearchActivity activity;
	static private List<String> data;
	private LayoutInflater inflater = null;
	public ImageLoader imageLoader;
	int pos;
	int currentapiVersion = Build.VERSION.SDK_INT;
	DisplayMetrics metrics;

	public SearchAdapter(SearchActivity mainActivity, List<String> d) {
		activity = mainActivity;
		data = d;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(activity.getApplicationContext());
		metrics = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
		
		
		
	}

	

	
	
	public int getCount() {
		return data.size() ;
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	@SuppressLint({ "ParserError" })
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolderHorizontal holderH = null;
		String ftitle = "";
		
		try {

			

			ftitle = SearchActivity.sectionsNameFromCache.getString(data
					.get(position));

		} catch (JSONException e) {

			e.printStackTrace();
		}
		
		if (convertView == null) {
			
		
				convertView = inflater.inflate(R.layout.horizontallistviewsearch,
						null);
				holderH = new ViewHolderHorizontal();
				holderH.hlvS = (HorizontalListView) convertView
						.findViewById(R.id.listview);

				holderH.FeedTextView = (TextView) convertView
						.findViewById(R.id.newstitletext);

				convertView.setTag(holderH);
			

		} else {

			holderH = (ViewHolderHorizontal) convertView.getTag();
		}
		
		
			
		
				
		String[] titles = new String[SearchActivity.List.get(position)._itemlist
				.size()];
		final String[] sections = new String[SearchActivity.List.get(position)._itemlist
				.size()];
		final String[] images = new String[SearchActivity.List.get(position)._itemlist
				.size()];
		String[] summary = new String[SearchActivity.List.get(position)._itemlist
				.size()];
		final String[] item = new String[SearchActivity.List.get(position)._itemlist
				.size()];
		
		for (int y = 0; y < SearchActivity.List.get(position)._itemlist.size(); y++) {
			titles[y] = SearchActivity.List.get(position)._itemlist.get(y)
					.getTitle();
			sections[y] = SearchActivity.List.get(position)._itemlist.get(y)
					.getCategory();
			images[y] = SearchActivity.List.get(position)._itemlist.get(y)
					.getIntroImage();
			summary[y] = SearchActivity.List.get(position)._itemlist.get(y)
					.getIntroText();
			item[y] = SearchActivity.List.get(position)._itemlist.get(y)
					.getItemId();
		}

		
		
		holderH.SAdapterHLV = new sAdapter(activity, item, titles, images,
				summary);
		
		holderH.hlvS.setAdapter(holderH.SAdapterHLV);
		/*
		MarginLayoutParams mlp = (MarginLayoutParams) holderH.hlvS
				.getLayoutParams();
		if (SearchActivity.isXLargeScreen(this.activity)) {
			if (SearchActivity.orientation == "portrait") {
				mlp.setMargins(
						-(int) (SearchActivity.GalleryWidth / 2 + (SearchActivity.unit * 2)),
						mlp.topMargin, mlp.rightMargin, mlp.bottomMargin);
			} else {
				mlp.setMargins(
						-(int) (SearchActivity.GalleryWidth / 2 + (SearchActivity.unit)),
						mlp.topMargin, mlp.rightMargin, mlp.bottomMargin);
			}
		} else {
			if (SearchActivity.orientation == "portrait") {
				holderH.hlvS.setSelection(1);
			} else {

				mlp.setMargins(
						-(int) (SearchActivity.GalleryWidth / 2 + (SearchActivity.unit)),
						mlp.topMargin, mlp.rightMargin, mlp.bottomMargin);
			}
		}

		holderH.hlvS.setLayoutParams(mlp);
        */
		final int pos = position;
		
		holderH.hlvS.setOnItemSelectedListener(new OnItemSelectedListener(){ 

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

				Bundle args = new Bundle();
				args.putInt("position", arg2);
				args.putInt("feed", pos);
				args.putString("itemID", item[arg2]);
				args.putString("section", sections[arg2]);
				try {
					args.putString("titlefeed",
							SearchActivity.sectionsNameFromCache.getString(data
									.get(pos)));
				} catch (JSONException e) {

					e.printStackTrace();
				}
				if (sections[arg2].equals("itwebvideo")) {

					Intent newIntent = new Intent(activity, VideoClass.class);
					Bundle argsVideo = new Bundle();
					argsVideo.putString("videoID", item[arg2]);
					newIntent.putExtras(argsVideo);
					activity.startActivity(newIntent);
				} else {
					Intent newIntent = new Intent(activity, SearchedNews.class);
					newIntent.putExtras(args);
					activity.startActivity(newIntent);
				}
			}
			
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}

		});
		
		

		try {
			holderH.FeedTextView.setText(SearchActivity.sectionsNameFromCache
					.getString(data.get(position)));
			holderH.FeedTextView.setTypeface(null, Typeface.BOLD);
		} catch (JSONException e) {

			e.printStackTrace();
		}
			
		
		return convertView;

	}

	private static class ViewHolderHorizontal {
		public HorizontalListView hlvS;
		public sAdapter SAdapterHLV;
		public TextView FeedTextView;

	}

}